package com.itfitness.ffmpegdemo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.graphics.PixelFormat;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.TextureView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.itfitness.ffmpegdemo.databinding.ActivityMainBinding;

import java.io.File;

public class MainActivity extends AppCompatActivity{
    private SurfaceView surfaceView;
    private Button button;
    static {
        System.loadLibrary("native-lib");
    }
    private SurfaceHolder mHolder;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        surfaceView = findViewById(R.id.sfv_player);
        button = findViewById(R.id.bt_play);
        button.setOnClickListener(v->{
            //找到SD卡中的视频文件
            File video = new File(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS),"001.mp4");
            //子线程进行视频渲染
            new Thread(new Runnable() {
                @Override
                public void run() {
                    native_start_play(video.getAbsolutePath(),mHolder.getSurface());
                }
            }).start();
        });
        initSurfaceHolder();
    }

    private void initSurfaceHolder() {
        mHolder = surfaceView.getHolder();
        mHolder.setFormat(PixelFormat.RGBA_8888);
    }

    /**
     * 调用NDK的视频渲染
     * @param path 播放的视频的路径
     * @param surface 要渲染的surface
     * @return
     */
    public native void native_start_play(String path, Surface surface);
}